/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.csit.iap.domain;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author Kundzia
 */
public class PDFWriter {

    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font magFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL, BaseColor.MAGENTA);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);
    private String file;
    private java.util.List<branchws.Order> order;
    private int bookstore_id;
    private Document doc;
    private double income;
    private int saled;
    private int orders;

    public PDFWriter(java.util.List<branchws.Order> order, int bookstore_id, String file) {
        String s  = new SimpleDateFormat("dd.MM.yyyy_HH.mm").format(new Timestamp(System.currentTimeMillis()));
        
        
        String date = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).
                concat(".").concat(String.valueOf(Calendar.getInstance().get(Calendar.MONTH) +1 )).
                concat(".").concat(String.valueOf(Calendar.getInstance().get(Calendar.YEAR))).
                concat("_").concat(String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY))).
                concat(":").concat(String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)));
        this.file = file + "_" + bookstore_id + "_" + s +".pdf";
        this.order = order;
        this.bookstore_id = bookstore_id;
        this.doc = new Document();
        this.income = 0;
        this.saled = 0;
        this.orders = 0;
    }

    public String createPDFReport() throws DocumentException, FileNotFoundException {
        FileOutputStream fos = new FileOutputStream("D:/Projects/BookstoreChainHQWS/web/" + file);
        PdfWriter.getInstance(doc, fos);
        doc.open();
        addMetaData(doc, bookstore_id);
        addTitlePage(doc);
        addContent(doc);
        addSummation(doc);

        doc.close();
        return file;
    }

    private void addMetaData(Document doc, int id) {
        doc.addTitle("Monthly report of BookStore nr " + id);
        doc.addCreationDate();
        doc.addAuthor("BookStoreChainHQ");
    }

    private void addTitlePage(Document doc) throws DocumentException {
        Paragraph title = new Paragraph("Report of BookStore nr " + bookstore_id, catFont);
        title.setAlignment(Element.ALIGN_CENTER);
        doc.add(title);

        Paragraph date = new Paragraph((Calendar.getInstance().get(Calendar.MONTH) + 1) + "." + Calendar.getInstance().get(Calendar.YEAR), smallBold);
        date.setAlignment(Element.ALIGN_CENTER);
        doc.add(date);
    }

    private void addContent(Document doc) throws DocumentException {
        Anchor anchor = new Anchor("Sales", catFont);
        anchor.setName("Sales list");

        Chapter sales = new Chapter(new Paragraph(anchor), 1);
        sales.setTriggerNewPage(false);

        createList(sales);

        doc.add(sales);
    }

    private void addSummation(Document doc) throws DocumentException {
        Anchor anchor = new Anchor("Summary", catFont);
        anchor.setName("Total");

        Chapter sum = new Chapter(new Paragraph(anchor), 2);
        sum.setTriggerNewPage(false);

        doc.add(sum);

        Paragraph par = new Paragraph();
        addEmptyLine(par, 1);

        doc.add(new Paragraph("Total number of orders: " + order.size(), magFont));
        doc.add(new Paragraph("Total number of saled books: " + saled, magFont));
        doc.add(new Paragraph("Total income from sales: " + income, magFont));
    }

    private void addEmptyLine(Paragraph par, int lines) {
        for (int i = 0; i < lines; i++) {
            par.add(new Paragraph(" "));
        }
    }

    private void createList(Section sec) throws DocumentException {
        List list = new List(true, false, 10);

        for (branchws.Order o : order) {
            orders++;
            ListItem li = new ListItem();
            li.add("Client: " + o.getClient().getName() + " " + o.getClient().getSurname());
            createTable(o, li);

            list.add(li);
        }

        sec.add(list);
    }

    private void createTable(branchws.Order order, ListItem li) {
        PdfPTable table = new PdfPTable(6);

        PdfPCell header = new PdfPCell(new Phrase("ISBN"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        header = new PdfPCell(new Phrase("TITLE"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        header = new PdfPCell(new Phrase("AUTHOR"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        header = new PdfPCell(new Phrase("AMOUNT"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        header = new PdfPCell(new Phrase("PRICE"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        header = new PdfPCell(new Phrase("TOTAL"));
        header.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(header);

        table.setHeaderRows(1);

        double total_sum = 0;

        for (branchws.OrderItem oi : order.getItems()) {
            table.addCell(oi.getBook().getIsbn());
            table.addCell(oi.getBook().getTitle());
            String authors = oi.getBook().getAuthors().get(0).getSurname();
            if (oi.getBook().getAuthors().size() > 1) {
                authors = (authors + " +...");
            }
            table.addCell(authors);
            int amount = oi.getQuantity();
            System.out.println("QUANTITY: " + oi.getQuantity() + " " + oi.getBook().getIsbn() + " id: " + order.getId());
            saled += amount;
            table.addCell(String.valueOf(amount));
            double price = oi.getBook().getPrice();
            table.addCell(String.valueOf(price));
            double total = price * amount;
            total_sum += total;
            table.addCell(String.valueOf(total));
        }

        income += total_sum;
        li.add(table);
        li.add("Total: " + total_sum);
    }
}
